using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace EncoderServiceConfig
{
    public partial class FormConfig : Form
    {
        const string REG_SUBKEY_NAME = "Software\\Maxwell Applications\\Encoding Service";
        const string KEYNAME_DVDRIPPATH = "DVDRipPath";
        const string KEYNAME_SAVEPATH = "SaveToPath";
        const string KEYNAME_DELETETIME = "DaysBeforeDelete";
        const string KEYNAME_INTERVAL = "HoursBetweenChecks";
        const string KEYNAME_HANDBRAKEPATH = "HandbrakePath";
        const string KEYNAME_VIDEOBITRATE = "VideoBitrate";
        const string KEYNAME_TWOPASS = "TwoPassEncoding";
        const string KEYNAME_TURBO = "TurboFirstPass";
        const string KEYNAME_TIMESTAMP = "SettingsChangedAt";

        public FormConfig()
        {
            InitializeComponent();
            openFileDialog.DefaultExt = "exe";
            openFileDialog.Multiselect = false;

            GetRegistryValues();
        }

        private void GetRegistryValues()
        {
            RegistryKey rkHKLM = Registry.LocalMachine;
            RegistryKey rkOpenKey;

            try
            {
                rkOpenKey = rkHKLM.CreateSubKey(REG_SUBKEY_NAME);

                if (rkOpenKey == null)
                    return;

                textBoxRipsDir.Text = (String)rkOpenKey.GetValue(KEYNAME_DVDRIPPATH, "");
                textBoxSaveTo.Text = (String)rkOpenKey.GetValue(KEYNAME_SAVEPATH, "");
                textBoxHbLocation.Text = (String)rkOpenKey.GetValue(KEYNAME_HANDBRAKEPATH, "");
                numUpDownCheckInterval.Value = (Int32)rkOpenKey.GetValue(KEYNAME_INTERVAL, 1);
                numUpDownDays.Value = (Int32)rkOpenKey.GetValue(KEYNAME_DELETETIME, 5);
                checkBoxTurbo.Checked = (Int32)rkOpenKey.GetValue(KEYNAME_TURBO, true) == 1;
                checkBoxTwoPass.Checked = (Int32)rkOpenKey.GetValue(KEYNAME_TWOPASS, true) == 1;
                sliderVideoBitrate.Value = (int)rkOpenKey.GetValue(KEYNAME_VIDEOBITRATE, 1500);
                numUpDownBitrate.Value = sliderVideoBitrate.Value;
            }
            catch (Exception e) {
                MessageBox.Show(e.Message);
                return;
            }
            
        }

        private void sliderVideoBitrate_Scroll(object sender, EventArgs e)
        {
            numUpDownBitrate.Value = sliderVideoBitrate.Value;
        }

        private void numUpDownBitrate_ValueChanged(object sender, EventArgs e)
        {
            sliderVideoBitrate.Value = (int)numUpDownBitrate.Value;
        }

        private void buttonRipsBrowse_Click(object sender, EventArgs e)
        {
            string folderResult = "", startFolder = "";

            if (textBoxRipsDir.Text != "" && System.IO.Directory.Exists(textBoxRipsDir.Text))
                startFolder = textBoxRipsDir.Text;

            folderResult = ShowFolderWindow(startFolder, "Select the folder where your DVD's are ripped to:");
            if (folderResult != "")
                textBoxRipsDir.Text = folderResult + "\\";
        }

        private string ShowFolderWindow(string startFolder, string description)
        {
            string selectedFolder = "";

            folderBrowserDialog.Description = description;
            if (startFolder != "")
                folderBrowserDialog.SelectedPath = startFolder;
            else
                folderBrowserDialog.RootFolder = System.Environment.SpecialFolder.MyComputer;

            DialogResult res = folderBrowserDialog.ShowDialog();
            if (res == DialogResult.OK)
            {
                selectedFolder = folderBrowserDialog.SelectedPath;
            }

            return selectedFolder;
        }

        private void buttonSaveBrowse_Click(object sender, EventArgs e)
        {
            string folderResult = "", startFolder = "";

            if (textBoxSaveTo.Text != "" && System.IO.Directory.Exists(textBoxSaveTo.Text))
                startFolder = textBoxSaveTo.Text;

            folderResult = ShowFolderWindow(startFolder, "Select the folder where you want your encoded movies saved to:");
            if (folderResult != "")
                textBoxSaveTo.Text = folderResult + "\\";
        }

        private void buttonHbBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog.Title = "Find Handbrake CLI EXE";

            if (textBoxSaveTo.Text != "" && System.IO.File.Exists(textBoxSaveTo.Text))
                openFileDialog.InitialDirectory = textBoxSaveTo.Text;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
                textBoxHbLocation.Text = openFileDialog.FileName;

        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            RegistryKey rkHKLM = Registry.LocalMachine;
            RegistryKey rkOpenKey;

            try
            {
                rkOpenKey = rkHKLM.CreateSubKey(REG_SUBKEY_NAME);

                if (rkOpenKey == null)
                    return;

                rkOpenKey.SetValue(KEYNAME_DVDRIPPATH, textBoxRipsDir.Text, RegistryValueKind.String);
                rkOpenKey.SetValue(KEYNAME_SAVEPATH, textBoxSaveTo.Text, RegistryValueKind.String);
                rkOpenKey.SetValue(KEYNAME_HANDBRAKEPATH, textBoxHbLocation.Text, RegistryValueKind.String);
                rkOpenKey.SetValue(KEYNAME_INTERVAL, numUpDownCheckInterval.Value, RegistryValueKind.DWord);
                rkOpenKey.SetValue(KEYNAME_DELETETIME, numUpDownDays.Value, RegistryValueKind.DWord);
                rkOpenKey.SetValue(KEYNAME_TURBO, checkBoxTurbo.Checked, RegistryValueKind.DWord);
                rkOpenKey.SetValue(KEYNAME_TWOPASS, checkBoxTwoPass.Checked, RegistryValueKind.DWord);
                rkOpenKey.SetValue(KEYNAME_VIDEOBITRATE, sliderVideoBitrate.Value, RegistryValueKind.DWord);

                rkOpenKey.SetValue(KEYNAME_TIMESTAMP, DateTime.Now.Ticks, RegistryValueKind.QWord);

                MessageBox.Show("Settings saved!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            
        }

        private void buttonRestore_Click(object sender, EventArgs e)
        {
            GetRegistryValues();
        }
    }
}